package com.ruimei.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruimei.back.BackMapper;
import com.ruimei.entitys.Notice;
import com.ruimei.mapper.NoticeMapper;
import com.ruimei.service.NoticeService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class NoticeServiceImpl
  implements NoticeService
{
  @Resource
  NoticeMapper noticeMapper;
  @Resource
  private BackMapper backMapper;
  
  @Cacheable(value={"myCache"}, key="'getSchoolNotice'+ #noticeId")
  public Map<String, Object> getSchoolNotice(String noticeId)
  {
    List<Notice> list = this.noticeMapper.getSchoolNotice(noticeId);
    
    Map<String, Object> map = new HashMap();
    map.put("status", Boolean.valueOf(true));
    map.put("message", "获取成功");
    map.put("count", Integer.valueOf(list.size()));
    map.put("data", list);
    
    return map;
  }
  
  @Cacheable(value={"myCache"}, key="'getClassNotice'+ #teacherId + #noticeId + #classId")
  public Map<String, Object> getClassNotice(String teacherId, String noticeId, String classId)
  {
    List<Notice> list = this.noticeMapper.getClassNotice(teacherId, noticeId, classId);
    Map<String, Object> map = new HashMap();
    map.put("status", Boolean.valueOf(true));
    map.put("message", "获取成功");
    map.put("count", Integer.valueOf(list.size()));
    map.put("data", list);
    
    return map;
  }
  
  @Cacheable(value={"myCache"}, key="'getPayNotice'+ #teacherId + #noticeId + #classId")
  public Map<String, Object> getPayNotice(String teacherId, String noticeId, String classId)
  {
    List<Notice> list = this.noticeMapper.getPayNotice(teacherId, noticeId, classId);
    
    Map<String, Object> map = new HashMap();
    map.put("status", Boolean.valueOf(true));
    map.put("message", "获取成功");
    map.put("count", Integer.valueOf(list.size()));
    map.put("data", list);
    return map;
  }
  
  public Map<String, Object> getSchoolList(String noticeId)
  {
    Map<String, Object> map = new HashMap();
    map.put("status", Boolean.valueOf(true));
    map.put("message", "获取成功");
    map.put("data", this.backMapper.selectNotice(noticeId));
    return map;
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public JSONObject sendNotice(Notice notice , RedisTemplate<String,String> redisTemplate) {
    JSONObject object = new JSONObject();
    JSONObject data = new JSONObject();
    //准备放入cache的容器
    Map<String,String> map = new HashMap<>();
    int row = noticeMapper.insertNotice(notice);
    if(row>0){
      List<String> stringList = noticeMapper.queryParents();
      //获取通知类型
      if("CLASS".equals(notice.getNoticeType().toString())){
        if(redisTemplate.hasKey("class_notice"+notice.getClassId())){
          redisTemplate.delete("class_notice"+notice.getClassId());
        }
        stringList.forEach(index -> {
          map.put(index,"0");
        });
        redisTemplate.opsForHash().putAll("class_notice"+notice.getClassId(),map);

        data.put("status",true);
        data.put("msg","班级通知发送成功");
        object.put("status",true);
        object.put("data",data);
        return object;
      }
      if("PAY_COST".equals(notice.getNoticeType().toString())){
        if(redisTemplate.hasKey("pay_notice"+notice.getClassId())){
          redisTemplate.delete("pay_notice"+notice.getClassId());
        }
        stringList.forEach(index -> {
          map.put(index,"0");
        });

        redisTemplate.opsForHash().putAll("pay_notice"+notice.getClassId(),map);
        data.put("status",true);
        data.put("msg","缴费通知发送成功");
        object.put("status",true);
        object.put("data",data);
        return object;

      }
    }
    data.put("status",false);
    data.put("msg","不是一个有效的通知类型");
    object.put("status",true);
    object.put("data",data);
    return object;
  }

  @Override
  public List<Notice> querySchoolNotice(String id, String type) {

    List<Notice> notices = noticeMapper.querySchoolNotice(id,type);

    return notices;
  }
}


/* Location:              E:\360MoveData\Users\admin\Desktop\teacher_wap-0.0.1-SNAPSHOT.jar!\BOOT-INF\classes\com\ruimei\service\impl\NoticeServiceImpl.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       0.7.1
 */